package main

import (
	"fmt"
	"time"
)

/*
golang定时器
*/
func main() {
	//1、time.Now实现
	ticker := time.NewTicker(time.Second)
	n := 5
	//ticker.C
	for t := range ticker.C {
		n--
		fmt.Println(t)
		if n == 0 {
			ticker.Stop() //终止定时器执行
			break
		}
	}

	//2、time.Sleep()  休眠
	fmt.Println("a1")
	time.Sleep(time.Second)
	fmt.Println("a2")
	time.Sleep(time.Second)
	fmt.Println("a3")
	time.Sleep(time.Second)
	fmt.Println("a4")

	for {
		time.Sleep(time.Second)
		fmt.Println("我在执行定时任务")
	}

}
